<template>
	<b-layout-index>
		<navbar is-style="light" :bgcolor="color.primary" color="#fff" :title="$t('购买商品细节')"></navbar>
		<u-action-sheet :cancelText="$t('取消')" :closeOnClickOverlay="true" :closeOnClickAction="true" :actions="list"
			round="10" :show="show" @select="selectClick" @close="closeClick"></u-action-sheet>
		<view class="pd-left-10 pd-right-10  font-size-12">

			<view v-if="order.cloudType==0">
				<b-address-dialog @action="getAddressAction" @actionType="getTypeAction" />
			</view>

			<shop-pay-input @action="actionPhone" @cancel="payShow=false" :price="shop.paymentAmount" :show="payShow" />
			<view class="bg-white mg-top-10 br-rd-10">
				<u-cell-group>
					<u-cell>
						<view slot="title">
							<text>{{$t('订单')}}</text>
						</view>
						<!-- 	<view slot="value">
							<text>{{time}}</text>
						</view> -->

					</u-cell>
					<u-cell v-for="(item,index) in shop.data">
						<view slot="icon" class="mg-5">
							<u--image width="40" height="40" :src="item.image" radius="10">
							</u--image>
						</view>
						<view slot="title">
							<text class=" font-size-12">{{item.name}}</text>
						</view>
						<view slot="value">
							<text class="font-size-12">{{item.quantity}}X</text>
						</view>
						<view slot="label" class="font-size-16">
							<text class="u-error font-bold">{{item.price}}￥</text>
						</view>
					</u-cell>

				</u-cell-group>

			</view>

			<!-- 	<view class="bg-white mg-top-10 br-rd-10">
				<u-cell isLink>
					<iconfont slot="icon" icon="icon-quan" size="30" :color="color.primary" />
					<view slot="title" class=" font-size-12">
						<text>{{$t('优惠券')}}</text>
					</view>
					<view slot="value" class=" font-size-12">
						<text class="u-error">{{$t("{0}个票",[3])}}</text>
					</view>
				</u-cell>

			</view> -->
			<view class="bg-white mg-top-10 br-rd-10">
				<u-cell-group>
					<u-cell>
						<view slot="title" class=" font-size-12">
							<text>{{$t('商品数量')}}</text>
						</view>
						<view slot="value" class=" font-size-12">
							<text>{{order.quantity}}</text>
						</view>
					</u-cell>
					<u-cell>
						<view slot="title" class=" font-size-12">
							<text>{{$t('优惠')}}</text>
						</view>
						<view slot="value" class=" font-size-12">
							<text class="u-error">0￥</text>
						</view>

					</u-cell>

					<u-cell>
						<view slot="title" class=" font-size-12">
							<text>{{$t('快递费')}}</text>
						</view>
						<view slot="value" class=" font-size-12">
							<text class="u-error">0￥</text>
						</view>
					</u-cell>
					<u-cell>
						<view slot="title" class=" font-size-12">
							<text>{{$t('云代发')}}</text>
						</view>
						<view slot="value" class=" font-size-12 ltr">
							<u-switch size="20" v-model="order.cloudType" :active-color="color.primary"
								inactive-color="#ccc"></u-switch>

						</view>
					</u-cell>
					<u-cell>
						<view slot="title" class=" font-size-12">
							<text>{{$t('备注')}}</text>
						</view>
						<view slot="label" class=" font-size-12">
							<u--textarea class="font-size-12" :maxlength="255" :placeholder="$t('请输入内容')"
								count></u--textarea>
						</view>
					</u-cell>
				</u-cell-group>
			</view>
		</view>

		<view class="bg-white sumview pd-10">

			<view class="sumbtn font-size-12 font-bold" @click="submit">{{$t('下单')}}</view>

			<view class="sumbtn1"><text>{{$t('支付金额')}}</text>:<text class="u-error">{{shop.paymentAmount}}￥</text>
			</view>
		</view>
	</b-layout-index>
</template>

<script>
	import {
		createNewOrder,
		createOrder
	} from "@/api/order.js"
	import {
		login,
		refreshLogin
	} from '@/api/oauth.js'
	import {
		findByInfo,
	} from "@/api/userWallet.js";
	export default {
		data() {
			var shopStorege = uni.getStorageSync("shopStorege")
			console.log(shopStorege)
			var newUser = uni.getStorageSync("newUser");
			return {
				shop: shopStorege,
				order: {
					isShop: 0,
					payType: 0,
					addressId: 0,
					shopId: 0,
					paymentAmount: shopStorege.paymentAmount,
					discountAmount: shopStorege.discountAmount,
					totalAmount: shopStorege.totalAmount,
					cloudType: shopStorege.cloudType || 0,
					quantity: shopStorege.quantity || 0,
					isUpgrade: shopStorege.isUpgrade || 0,
					userId: newUser.id
				},
				type: shopStorege.type || 0,
				agentId: shopStorege.agentId || 0,
				time: null,
				title: this.$t('标题'),
				list: [],
				show: false,
				payShow: false,
				myInfo: {
					balance: 0,
					integral: 0,
					servisefee: 0,
					taxes: 0,
					withdrawal: 0,
					withdrawalWaits: 0
				}

			}
		},
		onLoad: function() {
			this.time = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:M');
			this.getInfo()
		},
		methods: {
			submit: function() {
				var the = this;

				the.show = true
			},
			closeClick: function() {
				this.show = false
			},
			submitSendPay: function() {
				var the = this;
				var payType = the.order.payType;
				if(this.order.cloudType==true || this.order.cloudType==1){
					this.order.cloudType=1
				}else{
					this.order.cloudType=0
				}
				if (this.order.addressId > 0 || this.order.shopId > 0 || this.order.cloudType > 0) {
					uni.showLoading({

					})
					var param = {
						order: this.order,
						details: this.shop.data,
						agentId: this.agentId
					}
					if (this.type == 1) {

						createNewOrder(param).then(res => {
							if (payType == 1) {
								var a = refreshLogin()
								a.then(res => {
									uni.removeStorageSync("newUser")
									uni.setStorageSync("userInfo", res)
									uni.setStorageSync("Authorization", res.token)
									uni.reLaunch({
										url: "/pages/index/index"
									})
									uni.hideLoading()
								})
							} else {
								the.sendPay(res)
							}
						}).catch(e => {
							uni.hideLoading()
						})
					} else {
						createOrder(param).then(res => {
							console.log(res)
							if (payType == 1) {
								var a = refreshLogin()
								a.then(res => {
									uni.removeStorageSync("newUser")
									uni.setStorageSync("userInfo", res)
									uni.setStorageSync("Authorization", res.token)
									uni.reLaunch({
										url: "/pages/index/index"
									})
									uni.hideLoading()
								})
							} else {
								the.sendPay(res)
							}
						}).catch(e => {
							uni.hideLoading()
						})
					}
				} else {
					uni.$u.toast(this.$t('选择地址'))
				}
			},
			actionPhone: function() {
				var the = this;
				the.order.payType = 1;
				//the.order.payType = 0;
				the.submitSendPay()
			},
			sendPay: function(res) {
				uni.requestPayment({
					provider: 'wxpay',
					appId: res.appId,
					nonceStr: res.nonceStr,
					package: res.packageValue,
					paySign: res.paySign,
					signType: res.signType,
					timeStamp: res.timeStamp,
					success: function(res) {
						console.log('success:' + JSON.stringify(res));
						var a = refreshLogin()
						a.then(res => {
							uni.removeStorageSync("newUser")
							uni.setStorageSync("userInfo", res)
							uni.setStorageSync("Authorization", res.token)
							uni.reLaunch({
								url: "/pages/index/index"
							})
						})
						uni.hideLoading()
					},
					fail: function(err) {
						console.log('fail:' + JSON.stringify(err));
						uni.hideLoading()
					}
				});
			},
			getAddressAction: function(item) {
				if (this.order.isShop == 1) {
					this.order.shopId = item.id;
					this.order.addressId = 0
				} else {
					this.order.addressId = item.id
					this.order.shopId = 0
				}

			},
			getTypeAction: function(item) {
				//console.log(item)
				this.order.isShop = item.id;
			},
			selectClick: function(e) {
				var value = e.value;
				if (value == 1) {
					this.payShow = true;
				} else {
					this.submitSendPay()
				}
			},
			getInfo: function() {
				var info = this.myInfo;
				var the = this;
				findByInfo().then(res => {
					var i = Object.assign(info, res)
					the.myInfo = res
					the.list = [{
							value: 0,
							name: the.$t('微信支付')
						},
						{
							value: 1,
							name: the.$t('账号金额'),
							subname: the.$t('余额:{0}￥', [res.balance]),
							disabled: the.shop.paymentAmount > res.balance
						}
					]

					//	uni.setStorageSync("userWalletFindByInfo",res)
				})

			}
		}
	}
</script>

<style>
	.label {
		font-size: 14px;
		color: darkgray;
	}

	.sumview {
		height: 50px;
		width: 100%;
		position: fixed;
		bottom: 0px;
		display: flex;
		align-items: center;
		z-index: 100;

	}

	.sumbtn {
		width: 100px;
		background-color: var(--color-primary);
		padding-left: 10px;
		padding-right: 10px;
		padding-top: 5px;
		padding-bottom: 5px;
		text-align: center;
		border-radius: 30px;
		line-height: 30px;
		color: #fff;
		align-items: flex-start;
		align-content: flex-start;
	}

	.sumbtn1 {
		align-items: flex-end;
		align-content: flex-end;
		flex: 1;
		text-align: center;
		padding-left: 30px;
		padding-right: 10px;
	}
</style>